package top.lyc829.zzl_android.ui.profile.follow.user;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.UserInfoBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class FollowUserViewModel extends ViewModel {
    // TODO: Implement the ViewModel

    private MutableLiveData<List<UserInfoBean>> followUserLiveData;
    private MutableLiveData<Integer> isFollowLiveData;
    private MutableLiveData<UserInfoBean> userInfoBeanLiveData;

    public MutableLiveData<List<UserInfoBean>> getFollowUserLiveData() {
        if(followUserLiveData==null)
        {
            followUserLiveData=new MutableLiveData<>();
        }
        return followUserLiveData;
    }

    public MutableLiveData<Integer> getIsFollowLiveData() {
        if (isFollowLiveData == null) {
            isFollowLiveData = new MutableLiveData<>();
        }
        return isFollowLiveData;
    }

    public MutableLiveData<UserInfoBean> getUserInfoBeanLiveData()
    {
        if(userInfoBeanLiveData==null)
        {
            userInfoBeanLiveData=new MutableLiveData<>();
        }
        return userInfoBeanLiveData;
    }

    public void getFollowUserList(int userId)
    {
        Network.getFollowUserList(userId,new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr=res.body().string();
                ResultBean<List<UserInfoBean>> resultBean= JsonUtilNew.fromJsonArray(resultStr,UserInfoBean.class);
                if(resultBean.getCode().equals(200))
                {
                    getFollowUserLiveData().postValue(resultBean.getData());
                }
                else {
                    failed(new IOException(resultBean.getMsg()));
                    getFollowUserLiveData().postValue(null);
                }

            }

            @Override
            public void failed(IOException e) {

                getFollowUserLiveData().postValue(null);
                Utils.Sout(e.getMessage());
            }
        });
    }

}